home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr43 / ppl4p10.zip / SI.PAS < prev    next >
Pascal/Delphi Source File  |  1995-02-20  |  13KB  |  634 lines

  1. (*
  2. **
  3. **   --- please read this ! ---
  4. **
  5. **  This source code is in "shrouded" form. It is distributed in this form
  6. **  rather than as a library (.LIB) file because of the inconsistancies
  7. **  between object files generated by different compilers. To support several
  8. **  compilers would require a .LIB file for each compiler manufacturer, and
  9. **  sometimes several versions of the .LIB file are needed for the different
  10. **  versions of the same manufacturers compiler!
  11. **
  12. **  You can compile this code, but you will have to register with us in order
  13. **  to get the normal (commented) C source code with normal variable names.
  14. *)
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. {$I DEFINES.PAS}
  23.  
  24. unit SI;
  25.  
  26. interface
  27.  
  28. const
  29.   SI_CANNOT_OPEN          = -101;
  30.   SI_UNEXPECTED_EOF       = -102;
  31.   SI_NOT_SCRIPT_BINARY    = -103;
  32.   SI_NOT_CURRENT_VERSION  = -104;
  33.   SI_CODE_LENGTH_OVERFLOW = -105;
  34.   SI_DATA_LENGTH_OVERFLOW = -106;
  35.   SI_BAD_OPCODE           = -107;
  36.   SI_USER_ABORTS          = -108;
  37.   SI_STACK_OVERFLOW       = -109;
  38.   SI_STACK_UNDERFLOW      = -110;
  39.   SI_BAD_CHECKSUM         = -111;
  40.  
  41. procedure SaySiErr(V6:Integer);
  42. function  Script(Port:Integer;Filename:String;Debug:Boolean):Integer;
  43.  
  44. implementation
  45.  
  46. uses CRT, PCL4P, MODEM_IO, FILE_IO, OPCODES, TERM_IO, XYMODEM, XYPACKET, ZMODEM;
  47.  
  48. const
  49.    BUFFER_SIZE = 128;
  50.    CODE_SIZE   = 256;
  51.    DATA_SIZE   = 1024;
  52.    STACK_SIZE  = 32;
  53.    V55     = 2;
  54.  
  55. var  
  56.    Filename  : String;
  57.    V23    : File;
  58.    V26      : Integer;
  59.    V46     : Integer;
  60.    V48  : Integer;
  61.    V5  : Byte;
  62.    V10    : Integer;
  63.    V16    : Integer;
  64.    V34   : Char;
  65.    V35  : Integer;
  66.    V38   : Integer;
  67.    V36   : Integer;
  68.    V33   : Boolean;
  69.    V37: Char;
  70.    V2  : Integer;
  71.    V32: Integer;
  72.    V13  : Integer;
  73.    V49  : Integer;
  74.    V28   : Byte;
  75.    V29  : Boolean;
  76.    V7  : array[0..CODE_SIZE-1] of Byte;
  77.    V12  : array[0..DATA_SIZE-1] of Byte;
  78.    V4    : array[0..BUFFER_SIZE-1] of Byte;
  79.    V47 : array[0..STACK_SIZE-1] of Byte;
  80.  
  81. procedure SaySiErr(V6:Integer);
  82. begin
  83.   case V6 of
  84.     SI_CANNOT_OPEN:    WriteLn('Cannot open script binary');
  85.     SI_UNEXPECTED_EOF: WriteLn('Unexpected EOF');
  86.     SI_NOT_SCRIPT_BINARY:   WriteLn('Not script binary');
  87.     SI_NOT_CURRENT_VERSION: WriteLn('Incorrect script version');
  88.     SI_CODE_LENGTH_OVERFLOW:WriteLn('Code Overflow');
  89.     SI_DATA_LENGTH_OVERFLOW:WriteLn('Data Overflow');
  90.     SI_BAD_OPCODE:     WriteLn('Bad opcode encountered');
  91.     SI_USER_ABORTS:    WriteLn('User aborting...');
  92.     SI_STACK_OVERFLOW: WriteLn('Stack overflow');
  93.     SI_STACK_UNDERFLOW:WriteLn('Stack underflow');
  94.     SI_BAD_CHECKSUM:   WriteLn('Bad checksum');
  95.   else
  96.     WriteLn('Script Error ',V6);
  97.   end;
  98. end; 
  99.  
  100.  
  101. function V22:Integer;
  102. var
  103.   V54 : Byte;
  104. begin
  105.   if V26=V46 then
  106.   begin
  107.     
  108.     V26 := 0;
  109.     BlockRead(V23,V4,BUFFER_SIZE,V46);
  110.     if V46 <= 0 then
  111.     begin
  112.       V22 := -1;
  113.       exit;
  114.     end;
  115.   end;
  116.   
  117.   V54 := V4[V26];
  118.   V26 := V26 + 1;
  119.   V5 := V5 XOR V54;
  120.   V22 := V54;
  121. end;
  122.  
  123. function V41(Item:Integer):Integer;
  124. begin
  125.   if V48 = STACK_SIZE then V41 := SI_UNEXPECTED_EOF
  126.   else
  127.     begin
  128.       V47[V48] := Item;
  129.       V48 := V48 + 1;
  130.       V41 := 0
  131.     end;
  132. end;
  133.  
  134. function V39:Integer;
  135. begin
  136.   if V48=0 then V39 := SI_STACK_UNDERFLOW
  137.   else
  138.     begin
  139.       V48 := V48 - 1;
  140.       V39 := V47[V48]
  141.     end
  142. end;
  143.  
  144. function V21(V17:Integer) : Integer;
  145. const
  146.    V18 : array[1..10] of Integer =
  147.        ($180,$0C0,$060,$030,$018,$00C,$006,$003,$002,$001);
  148. var
  149.    i : Integer;
  150. begin
  151.    for i := 1 to 10 do if V18[i] = V17 then
  152.    begin
  153.      V21 := i - 1;
  154.      exit
  155.    end;
  156.    
  157.    V21 := -1;
  158. end;
  159.  
  160. function V45(V20:Boolean):Char;
  161. begin
  162.   if V20 then V45 := 'T'
  163.   else V45 := 'F';
  164. end;
  165.  
  166. function FetchText(V1:Integer):String;
  167. var
  168.   b : Byte;
  169.   s : String;
  170.   i : Integer;
  171. begin
  172.   s := '';
  173.   for i := 0 to 49 do
  174.   begin
  175.     b := V12[V1+i];
  176.     if b = 0 then
  177.     begin
  178.       FetchText := s;
  179.       exit;
  180.     end;
  181.     s := s + chr(b);
  182.   end
  183. end;
  184.  
  185. function FetchReal(V1:Integer):Real;
  186. var
  187.   V6    : Integer;
  188.   V53    : String;
  189.   V44 : Real;
  190. begin
  191.   V53 := FetchText(V1);
  192.   Val(V53,V44,V6);
  193.   FetchReal := V44;
  194. end;
  195.  
  196. function FetchInteger(V1:Integer):Integer;
  197. var
  198.   V6  : Integer;
  199.   V53  : String;
  200.   V25 : Integer;
  201. begin
  202.   V53 := FetchText(V1);
  203. {$R-}
  204.   Val(V53,V25,V6);
  205. {$R+}
  206.   FetchInteger := V25;
  207. end;
  208.  
  209.  
  210.  
  211. function  Script(Port:Integer;Filename:String;Debug:Boolean):Integer;
  212. var
  213.    i, k      : Integer;
  214.    c         : Char;
  215.    V54   : Byte;
  216.    V20      : Boolean;
  217.    V6      : Integer;
  218.    V30    : Integer;
  219.    V31      : Integer;
  220.    V1      : Integer;
  221.    V8   : Integer;
  222.    V14   : Integer;
  223.    V11: Integer;
  224.    V40       : Integer;
  225.    V24     : Integer;
  226.    Len       : Integer;
  227.    V53      : String;
  228.    V17   : Integer;
  229.    RealValue : Real;
  230.    IntegerValue:Integer;
  231.    Streaming : Boolean;
  232. begin
  233.   
  234.   V28 := Ord('C');
  235.   V29 := True;
  236.   V48 := 0;
  237.   V5 := 0;
  238.   V10 := 0;
  239.   V16 := 0;
  240.   V34 := chr($ff);
  241.   V35 := 1;
  242.   V36 := 5;
  243.   V33 := True;
  244.   V37 := 'X';
  245.   V38 := 18*30;
  246.   for i := 0 to CODE_SIZE-1 do V7[i] := 0;
  247.   for i := 0 to DATA_SIZE-1 do V12[i] := 0;
  248.   
  249.   V17 := SioGetDiv(Port);
  250.   V2 := V21(V17);
  251.   if V2 <= Baud19200 then Streaming := False
  252.   else Streaming := True;
  253.   V40 := SioRead(Port,3);
  254.   V13 := $03 AND V40;
  255.   V49 := $01 AND (V40 SHR 2);
  256.   V32 := $07 AND (V40 SHR 3);
  257.   
  258. {$I-}
  259.   Assign(V23,Filename+'.sb');
  260.   Reset(V23,1);
  261. {$I+}
  262.   if IOResult <> 0 then
  263.   begin
  264.     WriteLn('Cannot open ',Filename);
  265.     exit;
  266.   end;
  267.   V26 := 0;
  268.   V46 := 0;
  269.   
  270.   V6 := V22;
  271.   if V6 < 0 then
  272.   begin
  273.     Script := V6;
  274.     exit;
  275.   end;
  276.   if V6 <> $55 then
  277.   begin
  278.     Script := SI_NOT_SCRIPT_BINARY;
  279.     exit;
  280.   end;
  281.   
  282.   V6 := V22;
  283.   if V6 < 0 then
  284.   begin
  285.     Script := V6;
  286.     exit;
  287.   end;
  288.   if V6 <> V55 then
  289.   begin
  290.     Script := SI_NOT_CURRENT_VERSION;
  291.     exit;
  292.   end;
  293.   
  294.   V6 := V22;
  295.   if V6 < 0 then
  296.   begin
  297.     Script := V6;
  298.     exit;
  299.   end;
  300.   V8 := $FF AND V6;
  301.   V6 := V22;
  302.   if V6 < 0 then
  303.   begin
  304.     Script := V6;
  305.     exit;
  306.   end;
  307.   V8 := 256 * V8 + ($FF AND V6);
  308.   if V8 > CODE_SIZE then
  309.   begin
  310.     Script := SI_CODE_LENGTH_OVERFLOW;
  311.     exit;
  312.   end;
  313.   
  314.   V6 := V22;
  315.   if V6 < 0 then
  316.   begin
  317.     Script := V6;
  318.     exit;
  319.   end;
  320.   V14 := $FF AND V6;
  321.   V6 := V22;
  322.   if V6 < 0 then
  323.   begin
  324.     Script := V6;
  325.     exit;
  326.   end;
  327.   V14 := 256 * V14 + ($FF AND V6);
  328.   if V14 > DATA_SIZE then
  329.   begin
  330.     Script := SI_DATA_LENGTH_OVERFLOW;
  331.     exit;
  332.   end;
  333.   
  334.   for i := 0 to V8-1 do
  335.   begin
  336.     V6 := V22;
  337.     if V6 < 0 then
  338.     begin
  339.       Script := V6;
  340.       exit;
  341.     end;
  342.     V7[i] := V6;
  343.   end;
  344.   
  345.   V6 := V22;
  346.   if V6 < 0 then
  347.   begin
  348.     Script := V6;
  349.     exit;
  350.   end;
  351.   if V6 <> $55 then
  352.   begin
  353.     Script := SI_NOT_SCRIPT_BINARY;
  354.     exit;
  355.   end;
  356.   
  357.   for i := 0 to V14-1 do
  358.   begin
  359.     V6 := V22;
  360.     if V6 < 0 then
  361.     begin
  362.       Script := V6;
  363.       exit;
  364.     end;
  365.     V12[i] := V6;
  366.   end;
  367.   
  368.   V6 := V22;
  369.   if V6 < 0 then
  370.   begin
  371.     Script := V6;
  372.     exit;
  373.   end;
  374.   if V6 <> $55 then
  375.   begin
  376.     Script := SI_NOT_SCRIPT_BINARY;
  377.     exit;
  378.   end;
  379.   
  380.   V11 := V5;
  381.   V6 := V22;
  382.   if V6 < 0 then
  383.   begin
  384.     Script := V6;
  385.     exit;
  386.   end;
  387.   if V6 <> V11 then
  388.   begin
  389.     Script := SI_BAD_CHECKSUM;
  390.     exit;
  391.   end;
  392.   
  393.   V6 := SioRxFlush(Port);
  394.   V6 := SioTxFlush(Port);
  395.   V10 := 0;
  396.   repeat
  397.     
  398.     if SioBrkKey OR KeyPressed then
  399.     begin
  400.       Write('Aborted by user...');
  401.       V6 := SioDone(Port);
  402.       exit;
  403.     end;
  404.     
  405.     V54 := V7[V10];
  406.     V30 := $003F AND V54;
  407.     V31 := ($00C0 AND V54) SHL 2;
  408.     if Debug then
  409.     begin
  410.       V24 := MatchOpCode(V30);
  411.       if WhereX > 1 then WriteLn;
  412.       Write('@',V10,'  ');
  413.       Write(GetOpText(V24),' ');
  414.     end;
  415.     V10 := V10 + 1;
  416.     
  417.     if V30 >= 8 then
  418.     begin
  419.       V1 := V31 OR ($00FF AND V7[V10]);
  420.       V10 := V10 + 1;
  421.       if Debug then
  422.       begin
  423.         case GetOperType(V24) of
  424.           CODE_REF: WriteLn(V1);
  425.           DATA_REF:
  426.             begin
  427.               Write('"');
  428.               i := 0;
  429.               Repeat
  430.                 k := V12[V1+i];
  431.                 i := i + 1;
  432.                 if k <> 0 then Write(chr(k));
  433.               until k = 0;
  434.               Writeln('"');
  435.             end
  436.         end 
  437.       end
  438.     end;
  439.     
  440.     case V30 of
  441.       OPC_HALT:
  442.         begin
  443.           Script := 0;
  444.           exit
  445.         end;
  446.       OPC_STATUS:
  447.         begin
  448.           Write('CodePC=',V10,' V30=',V30,' V1=',V1);
  449.           Write(' PSC=',V34,' Count=',V35);
  450.           WriteLn(' Wait=',V38,' V37=',V37);
  451.         end;
  452.       OPC_DELAY:
  453.         begin
  454.           IntegerValue := Round(18.2*FetchReal(V1));
  455.           SioDelay( IntegerValue );
  456.         end;
  457.       OPC_CALL:
  458.         begin
  459.           V6 := V41(V10);
  460.           if V6 < 0 then
  461.             begin
  462.               Script := V6;
  463.               exit;
  464.             end;
  465.           V10 := V1;
  466.         end;
  467.       OPC_RETURN:
  468.         begin
  469.           V6 := V39;
  470.           if V6 < 0 then
  471.             begin
  472.               Script := V6;
  473.               exit;
  474.             end;
  475.           V10 := V6;
  476.         end;
  477.       OPC_BAUD:
  478.         begin
  479.           V53 := FetchText(V1);
  480.           i := MatchBaud(V53);
  481.         end;
  482.       OPC_DATABITS:
  483.         begin
  484.           i := FetchInteger(V1);
  485.           case i of
  486.             7: V13 := WordLength7;
  487.             8: V13 := WordLength8;
  488.           end;
  489.           V6 := SioParms(Port,V32,V49,V13);
  490.         end;
  491.       OPC_STOPBITS:
  492.         begin
  493.           i := FetchInteger(V1);
  494.           case i of
  495.             1: V49 := OneStopBit;
  496.             2: V49 := TwoStopBits;
  497.           end;
  498.           V6 := SioParms(Port,V32,V49,V13);
  499.         end;
  500.       OPC_PARITY:
  501.         begin
  502.           V53 := FetchText(V1);
  503.           case UpCase(V53[1]) of
  504.             'N': V32 := NoParity;
  505.             'O': V32 := OddParity;
  506.             'E': V32 := EvenParity;
  507.           end;
  508.           V6 := SioParms(Port,V32,V49,V13);
  509.         end;
  510.       OPC_REPLY:
  511.         begin
  512.           V53 := FetchText(V1);
  513.           if ModemSendTo(Port,V36,V53) then V34 := chr($ff)
  514.           else V34 := chr($00);
  515.         end;
  516.       OPC_SETCOUNT:
  517.         V35 := FetchInteger(V1);
  518.       OPC_SETWAIT:
  519.         begin
  520.           IntegerValue := Round(18.2*FetchReal(V1));
  521.           V38 := IntegerValue;
  522.         end;
  523.       OPC_LOOP:
  524.         begin
  525.           V35 := V35 - 1;
  526.           if V35 > 0 then V10 := V1
  527.         end;
  528.       OPC_IFTRUE:
  529.         if V34 <> chr($00) then V10 := V1;
  530.       OPC_IFFALSE:
  531.         if V34 = chr($00) then V10 := V1;
  532.       OPC_IF:
  533.         if V34 <> chr(V12[V1]) then V10 := V10 + 2;
  534.       OPC_IFNOT:
  535.         if V34 = chr(V12[V1]) then V10 := V10 + 2;
  536.       OPC_TEST:
  537.         V34 := chr(V12[V1]);
  538.       OPC_ACCEPT:    
  539.         begin
  540.           ReadMsg(V53,61,15);
  541.           Len := Length(V53);
  542.           for i := 0 to Len-1 do V12[V1+i] := Byte(V53[i+1]);
  543.           V12[V1+Len] := $00;
  544.         end;
  545.       OPC_GOTO:
  546.         V10 := V1;
  547.       OPC_SAY:
  548.         begin
  549.           V53 := FetchText(V1);
  550.           i := 1;
  551.           while i <= Length(V53) do
  552.             begin
  553.               c := V53[i];
  554.               i := i + 1;
  555.               if c = '^' then
  556.                 begin
  557.                   c := chr( Byte(V53[i]) - $40);
  558.                   i := i + 1;
  559.                 end;
  560.               Write(c);
  561.             end;
  562.           WriteLn;
  563.         end;
  564.       OPC_WAITFOR:
  565.         begin
  566.           V53 := FetchText(V1);
  567.           V34 := ModemWaitFor(Port,V38,V33,V53);
  568.         end;
  569.       OPC_NOP:
  570.         begin
  571.         end;
  572.       OPC_SETPACE:
  573.         V36 := Round(18.2*FetchReal(V1));
  574.       OPC_SETCASE:
  575.         begin
  576.           V53 := FetchText(V1);
  577.           case UpCase(V53[1]) of
  578.             'T': V33 := True;
  579.             'F': V33 := False;
  580.           end;
  581.         end;
  582.       OPC_QUIET:
  583.         begin
  584.           IntegerValue := Round(18.2*FetchReal(V1));
  585.           ModemQuiet(Port, IntegerValue);
  586.         end;
  587.       OPC_HANGUP:
  588.         ModemHangup(Port);
  589.       OPC_PROTOCOL:
  590.         begin
  591.           V53 := FetchText(V1);
  592.           case UpCase(V53[1]) of
  593.             'X': V37 := 'X';
  594.             'Y': V37 := 'Y';
  595.             'Z': V37 := 'Z';
  596.           end;
  597.         end;
  598.       OPC_SEND:
  599.         begin
  600.           ModemEcho(Port,10);
  601.           V53 := FetchText(V1);
  602.           case V37 of
  603.             'X': V20 := XmodemTx(Port,V53,V29);
  604.             'Y': V20 := YmodemTx(Port,V53,V29);
  605.             'Z': V20 := ZmodemTx(Port,V53,Streaming)
  606.           end;
  607.         end;
  608.       OPC_RECEIVE:
  609.         begin
  610.           ModemEcho(Port,10);
  611.           case V37 of
  612.             'X':
  613.               begin
  614.                 V53 := FetchText(V1);
  615.                 V20 := XmodemRx(Port,V53,V28)
  616.               end;
  617.             'Y':
  618.               begin
  619.                 V53 := '';
  620.                 V20 := YmodemRx(Port,V53,V28)
  621.               end;
  622.             'Z':
  623.               begin
  624.                 V53 := '';
  625.                 V20 := ZmodemRx(Port,V53,Streaming)
  626.               end
  627.             end
  628.         end;
  629.     end; 
  630.  until False
  631. end; 
  632.  
  633. end.
  634.